var Blemash = require("../BleMash-js/blemash.js");
var Ajax = require("../../utils/ajax.js")
var Modal = require("../../utils/modal.js")
var app = getApp();
Page({
  data: {
    currentpage: 0,
  },

  onLoad() {
    this.setData({
      phoneheight: app.globalData.phoneheight,
      fix_position: app.globalData.fix_position,
      phonewidth: app.globalData.phonewidth,
      rpxRatio: app.globalData.phonewidth / 750
    })
    Ajax.AsyncPost('/grant/grantee_and_meshs/', {
      A_id: app.globalData.activemember.A_id,
      B_id: app.globalData.activemember.B_id
    }, (data) => {
      this.data.meshsin = data.granted;
      this.data.meshsout = data.ungranted;
      this.setData({
        meshsin: data.granted,
        meshsout: data.ungranted
      });
    })
  },
  bindchange: function (e) {
    this.setData({
      currentpage: e.detail.current
    })
  },
  checkcurrent: function (e) {
    if (this.data.currentpage != e.target.dataset.current) {
      this.setData({
        currentpage: e.target.dataset.current
      })
    }
  },
  letin(e) {
    var index = e.currentTarget.dataset.index;
    Modal.showModal("确认将 " + this.data.meshsout[index].name + " 授权给该用户吗？",()=>{
      Ajax.AsyncPost('/grant/add_mesh_to_member/', {
        A_id: app.globalData.activemember.A_id,
        B_id: app.globalData.activemember.B_id,
        mesh_id: this.data.meshsout[index].id,
      }, (data) => {
        let ungranted = this.data.meshsout.splice(index, 1);
        this.data.meshsin.push(ungranted[0]);
        this.setData({
          meshsin: this.data.meshsin ,
          meshsout: this.data.meshsout
        });
      })
    });
  },
  letout(e) {
    var index = e.currentTarget.dataset.index;
    Modal.showModal("确认收回对该用户 " + this.data.meshsin[index].name + " 的授权吗？", () => {
      Ajax.AsyncPost('/grant/delete_mesh_of_member/', {
      A_id: app.globalData.activemember.A_id,
      B_id: app.globalData.activemember.B_id,
      mesh_id: this.data.meshsin[index].id,
      }, (data) => {
        let granted = this.data.meshsin.splice(index, 1);
        this.data.meshsout.push(granted[0]);
        this.setData({
          meshsin: this.data.meshsin,
          meshsout: this.data.meshsout
        });
      })
    });
  }

})
